package com.key.service.impl;

import com.github.pagehelper.PageHelper;
import com.key.common.dto.ClearFormFindDTO;
import com.key.common.vo.ClearFormVO;
import com.key.entity.ClearForm;
import com.key.mapper.ClearFormMapper;
import com.key.service.ClearFormService;
import com.key.utils.ExcelExportHelper;
import com.key.utils.OddNumbersUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author wei
 * @create 2023-02-22 9:47
 */
@Service
public class ClearFormServiceImpl implements ClearFormService {

    @Autowired
    ClearFormMapper clearFormMapper;

    @Override
    @Transactional
    public boolean addClearForm(ClearForm clearForm) {
        String cfNo = OddNumbersUtils.creatON("QL");

        List<Long> assetsIdList = clearForm.getAssetsIdList();

        clearForm.setCfNo(cfNo);
        //设置状态为未处理
        clearForm.setCfState(1);

        int i = clearFormMapper.insertClearForm(clearForm);
        int j = 0;
        for(Long assetsId : assetsIdList){
            j = clearFormMapper.insertAssetsClear(cfNo, assetsId);
        }

        return i > 0 && j > 0;
    }

    @Override
    @Transactional
    public boolean removeClearForm(String cfNo) {

        int i = clearFormMapper.deleteAssetsClearByCfNo(cfNo);

        int j = clearFormMapper.deleteClearFormByCfNo(cfNo);

        return i > 0 && j > 0;
    }

    @Override
    @Transactional
    public boolean editClearForm(ClearForm clearForm) {

        //修改清理报废单信息
        int a = clearFormMapper.updateClearForm(clearForm);
        int b = 0;
        int c = 0;

        if(a != 0){
            //获取单号
            String cfNo = clearForm.getCfNo();

            //根据单号删除中间表信息
            b = clearFormMapper.deleteAssetsClearByCfNo(cfNo);

            //获取新的资产id List
            List<Long> assetsIdList = clearForm.getAssetsIdList();

            //新的资产列表添加进中间表
            for(Long assetsId : assetsIdList){
                c = clearFormMapper.insertAssetsClear(cfNo, assetsId);
            }

            //如果前端将单子的办理状态设置为已处理
            if(clearForm.getCfState() == 2){
                clearFormMapper.updateAssetsState(cfNo);
            }
        }

        return a > 0 && b > 0 && c > 0;
    }

    @Override
    public Map<String, Object> findClearForm(ClearFormFindDTO clearFormFindDTO) {

        Map<String, Object> map = new HashMap<>();

        List<ClearForm> clearFormList;

        if(clearFormFindDTO.getCurrentPage() != null && clearFormFindDTO.getPageSize() != null){
            //分页
            PageHelper.startPage(clearFormFindDTO.getCurrentPage(), clearFormFindDTO.getPageSize());
            clearFormList = clearFormMapper.selectClearForm(clearFormFindDTO);
        }else {
            //不分页
            clearFormList = clearFormMapper.selectClearForm(clearFormFindDTO);
        }

        Integer count = clearFormMapper.getClearFormCount(clearFormFindDTO);

        map.put("clearFormList", clearFormList);
        map.put("count", count);

        return map;
    }

    @Override
    public ClearFormVO findClearFormInfoByCfNo(String cfNo) {

        ClearFormVO clearFormVO = clearFormMapper.selectClearFormByCfNo(cfNo);

        clearFormVO.setAssetsList(clearFormMapper.selectAssetsByCfNo(cfNo));

        return clearFormVO;
    }

    @Override
    public HSSFWorkbook getWorkBook() {
        String[] header = {"报废清单主键ID", "报废清单单号", "办理状态(1:未处理，2:已处理)", "处理人", "清理日期", "清理原因","业务所属公司","照片"};
        String[] properties = {"cfId", "cfNo", "cfState", "cfProcessor", "cfClearTime", "cfClearReason", "cfCompany", "cfImage"};
        List<ClearForm> clearFormList = clearFormMapper.selectEndClearFormList();
        List list = new ArrayList();
        for (ClearForm clearForm : clearFormList) {
            list.add(clearForm);
        }
        ExcelExportHelper excelHelper = new ExcelExportHelper();
        HSSFWorkbook workbook = excelHelper.exportExcel(header, properties, list, "清理报废列表");
        return workbook;

    }
}
